package com.cxg.fairy.ec.main.personal.order;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.ec.detail.CommentActionDelegate;
import com.cxg.fairy.ec.main.order.CheckPayDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.recycler.HorizontalListView.HorizontalListAdapter;
import com.cxg.fairy.ui.recycler.HorizontalListView.HorizontalListView;
import com.cxg.fairy.ui.recycler.HorizontalListView.OrderListHorizontalImageData;
import com.diabin.latte.ec.R;
import com.cxg.fairy.ui.recycler.MultipleFields;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.ui.recycler.MultipleRecyclerAdapter;
import com.cxg.fairy.ui.recycler.MultipleViewHolder;
import com.mylhyl.superdialog.SuperDialog;

import java.math.BigDecimal;
import java.util.List;


/**
 * Created by CFairy
 */

public class OrderListAdapter extends MultipleRecyclerAdapter {

    private OrderListEntryDelegate mDelegate = null;

    private AppCompatButton cancelBtn = null;

    private AppCompatButton actionBtn = null;

    private AppCompatTextView tvOrderStatus = null;

    private String orderNo = "";

    public void setmDelegate(OrderListEntryDelegate mDelegate) {
        this.mDelegate = mDelegate;
    }

    protected OrderListAdapter(List<MultipleItemEntity> data) {
        super(data);
        addItemType(OrderListItemType.ITEM_ORDER_LIST, R.layout.item_order_list);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(MultipleViewHolder holder, MultipleItemEntity entity) {
        super.convert(holder, entity);
        switch (holder.getItemViewType()) {
            case OrderListItemType.ITEM_ORDER_LIST:
                HorizontalListView mHlvCustomList = holder.getView(R.id.hlvSimpleList);

                AppCompatImageView imageView = holder.getView(R.id.image_order_list);
                AppCompatTextView title = holder.getView(R.id.atv_order_list_shop_name);
                AppCompatTextView price = holder.getView(R.id.tv_order_list_price);
                AppCompatTextView orderGoodsCount = holder.getView(R.id.atv_order_list_count);
                AppCompatTextView orderListTime = holder.getView(R.id.atv_order_list_order_time);
                cancelBtn = holder.getView(R.id.btn_order_list_cancel);
                actionBtn = holder.getView(R.id.btn_order_list_action);
                tvOrderStatus = holder.getView(R.id.atv_order_list_status);

                final int total = entity.getField(OrderItemFields.TOTAL);
                String titleVal = entity.getField(MultipleFields.TITLE);
                String timeVal = entity.getField(OrderItemFields.TIME);
                final BigDecimal priceVal = entity.getField(OrderItemFields.PRICE);
                JSONArray imgArray = entity.getField(MultipleFields.IMAGE_URL);
                int status = entity.getField(OrderItemFields.STATUS);
                int eStatus = entity.getField(OrderItemFields.ESTATUS);
                orderNo = entity.getField(OrderItemFields.ORDERNO);
                final int orderType = entity.getField(OrderItemFields.ORDERTYPE);

                final OrderListHorizontalImageData[] mOrderListHorizontalImageData = new OrderListHorizontalImageData[imgArray.size()];
                for (int i =0;i< imgArray.size();i++) {
                    try {
                        mOrderListHorizontalImageData[i] = new OrderListHorizontalImageData(imgArray.get(i).toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                HorizontalListAdapter adapter = new HorizontalListAdapter(holder.itemView.getContext(), mOrderListHorizontalImageData);
                mHlvCustomList.setAdapter(adapter);

                cancelBtn.setVisibility(View.INVISIBLE);

                switch (orderType) {
                    case 0:
                        switch (status) {
                            case 1:
                                status1();
                                break;
                            case 2:
                                status2();
                                break;
                            case 3:
                                status3();
                                break;
                            case 4:
                                status4(orderNo);
                                break;
                            case 5:
                                status5();
                                break;
                            case 6:
                                status6();
                                break;
                            case 7:
                                status7();
                                break;
                            case 8:
                                status8();
                                break;
                             default:
                                 break;
                        }
                        break;
                    case 1:
                         status1();
                         break;
                    case 2:
                        status2();
                        break;
                    case 3:
                        status3();
                        break;
                    case 4:
                        status4(orderNo);
                        break;
                    case 5:
                        actionBtn.setText("申请售后");
                        actionBtn.setVisibility(View.INVISIBLE);
                        switch (status) {
                            case 2:
                                tvOrderStatus.setText("准备发货");
                                actionBtn.setVisibility(View.VISIBLE);
                                actionBtn.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        AfterSalesDelegate delegate = AfterSalesDelegate.create(orderNo);
                                        delegate.setmOrderListHorizontalImageData(mOrderListHorizontalImageData);
                                        delegate.setOrderAmount(priceVal);
                                        delegate.setTotal(total);
                                        mDelegate.getSupportDelegate().start(delegate);
                                    }
                                });
                                break;
                            case 3:
                                tvOrderStatus.setText("正在配送");
                                actionBtn.setVisibility(View.VISIBLE);
                                actionBtn.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        AfterSalesDelegate delegate = AfterSalesDelegate.create(orderNo);
                                        delegate.setmOrderListHorizontalImageData(mOrderListHorizontalImageData);
                                        delegate.setOrderAmount(priceVal);
                                        delegate.setTotal(total);
                                        mDelegate.getSupportDelegate().start(delegate);
                                    }
                                });
                                break;
                            case 4:
                                tvOrderStatus.setText("交易完成");
                                actionBtn.setVisibility(View.VISIBLE);
                                actionBtn.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        AfterSalesDelegate delegate = AfterSalesDelegate.create(orderNo);
                                        delegate.setmOrderListHorizontalImageData(mOrderListHorizontalImageData);
                                        delegate.setOrderAmount(priceVal);
                                        delegate.setTotal(total);
                                        mDelegate.getSupportDelegate().start(delegate);
                                    }
                                });
                                break;

                            case 5:
                                tvOrderStatus.setText("售后处理中");
                                break;
                            case 6:
                                tvOrderStatus.setText("售后关闭");
                                break;
                            case 7:
                                tvOrderStatus.setText("售后已拒绝");
                                break;
                            default:
                                break;
                        }
                        break;
                    default:
                        break;
                }

                title.setText(titleVal);
                orderListTime.setText(timeVal);
                orderGoodsCount.setText("共计"+total+"件商品");
                price.setText(String.valueOf(priceVal));
                break;
            default:
                break;
        }
    }

    private void status1() {
        tvOrderStatus.setText("未支付");
        cancelBtn.setVisibility(View.VISIBLE);
        actionBtn.setText("去支付");
        actionBtn.setVisibility(View.VISIBLE);
        actionBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDelegate.getSupportDelegate().start(new CheckPayDelegate().setIsDeposit(false).setOrderNo(orderNo));
            }
        });
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new SuperDialog.Builder(mDelegate.getActivity())
                        .setRadius(10)
                        .setAlpha(0.9f)
                        .setWidth(0.6f)
                        .setTitle("取消订单", 40, 80)
                        .setMessage("确认取消当前订单吗?", Color.parseColor("#545454"), 40)
                        .setPositiveButton("确认", new SuperDialog.OnClickPositiveListener() {
                            @Override
                            public void onClick(View v) {
                                RestClient.builder()
                                        .url("order/cancelOrder")
                                        .params("uuid", AccountManager.getSignUUID())
                                        .params("orderNo",orderNo)
                                        .loader(mDelegate.getContext())
                                        .success(new ISuccess() {
                                            @Override
                                            public void onSuccess(String response) {
                                                int code = JSON.parseObject(response).getInteger("code");
                                                if (code == 1) {
                                                    Toast.makeText(mDelegate.getContext(),"订单取消成功!",Toast.LENGTH_SHORT).show();
                                                    mDelegate.adapter.notifyDataSetChanged();
                                                }
                                            }
                                        })
                                        .build()
                                        .post();
                            }
                        })
                        .setNegativeButton("取消", new SuperDialog.OnClickNegativeListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        })
                        .build();
            }
        });
    }

    private void status2() {
        tvOrderStatus.setText("准备发货");
        actionBtn.setVisibility(View.INVISIBLE);
    }

    private void status3() {
        tvOrderStatus.setText("正在配送");
        actionBtn.setText("确认收货");
        actionBtn.setVisibility(View.VISIBLE);
        actionBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RestClient.builder()
                        .url("order/confirmReceipt")
                        .params("uuid", AccountManager.getSignUUID())
                        .params("orderNo",orderNo)
                        .loader(mDelegate.getContext())
                        .success(new ISuccess() {
                            @Override
                            public void onSuccess(String response) {
                                int code = JSON.parseObject(response).getInteger("code");
                                if (code == 1) {
                                    Toast.makeText(mDelegate.getContext(),"确认收货完成!",Toast.LENGTH_SHORT).show();
                                    mDelegate.adapter.notifyDataSetChanged();
                                }
                            }
                        })
                        .build()
                        .post();
            }
        });
    }

    private void status4(final String mOrderNo) {
        tvOrderStatus.setText("已完成");
        actionBtn.setText("去评价");
        actionBtn.setVisibility(View.VISIBLE);
        actionBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDelegate.getSupportDelegate().start(CommentActionDelegate.create(mOrderNo));
            }
        });
    }

    private void status5() {
        tvOrderStatus.setText("售后处理中");
        actionBtn.setVisibility(View.INVISIBLE);
        cancelBtn.setVisibility(View.INVISIBLE);
    }

    private void status6() {
        tvOrderStatus.setText("售后关闭");
    }

    private void status7() {
        tvOrderStatus.setText("售后拒绝");
    }

    private void status8() {
        tvOrderStatus.setText("已取消订单");
    }

}
